package com.sunlands.deskmate.common.thread;

import org.slf4j.MDC;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

/**
 * @ClassName ThreadPoolTaskExecutorDecorator
 * @Destription 线程重写类
 * @AUTHOR anjl
 *
 * @VERSION 1.0
 **/
public class ThreadPoolTaskExecutorDecorator extends ThreadPoolTaskExecutor {

    private static final long serialVersionUID = -9132702922468298050L;

    @Override
    public <T> Future<T> submit(Callable<T> task) {
        // 传入线程池之前先复制当前线程的MDC
        return super.submit(MdcRunable.wrap(task, MDC.getCopyOfContextMap()));
    }

    @Override
    public Future<?> submit(Runnable task) {
        return super.submit(MdcRunable.wrap(task, MDC.getCopyOfContextMap()));
    }

    @Override
    public void execute(Runnable task) {
        super.execute(MdcRunable.wrap(task, MDC.getCopyOfContextMap()));
    }
}